{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# 量子模拟器\n",
        "\n",
        "[![下载Notebook](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_notebook.png)](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/master/mindquantum/zh_cn/mindspore_quantum_simulator.ipynb)&emsp;\n",
        "[![下载样例代码](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_download_code.png)](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/master/mindquantum/zh_cn/mindspore_quantum_simulator.py)&emsp;\n",
        "[![查看源文件](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_source.png)](https://gitee.com/mindspore/docs/blob/master/docs/mindquantum/docs/source_zh_cn/quantum_simulator.ipynb)\n",
        "\n",
        "## 概述\n",
        "\n",
        "搭建出量子线路后，我们需要指定一个后端来运行量子线路，在MindQuantum中，我们可以利用量子模拟器`Simulator`来对量子线路进行模拟运行。在本教程中我们声明一个两比特的`projectq`模拟器，并以此来简介模拟器的关键功能。\n",
        "\n",
        "## 环境准备\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "导入本教程所依赖的模块。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np                             # 导入numpy库并简写为np\n",
        "from mindquantum.simulator import Simulator    # 从mindquantum.simulator中导入Simulator类\n",
        "from mindquantum.core import X, H, RY          # 导入量子门H, X, RY"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "说明：\n",
        "\n",
        "（1）numpy是一个功能强大的Python库，主要用于对多维数组执行计算，支持大量的维度数组与矩阵运算，此外也针对数组运算提供大量的数学函数库；\n",
        "\n",
        "（2）mindquantum是量子-经典混合计算框架，支持多种量子神经网络的训练和推理；\n",
        "\n",
        "（3）搭建的量子线路中所需执行的量子门需要从mindquantum.core模块中导入；"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "projectq simulator with 2 qubits (little endian).\n",
              "Current quantum state:\n",
              "1¦00⟩"
            ]
          },
          "execution_count": 2,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "sim = Simulator('projectq', 2)   #声明一个两比特的projectq模拟器\n",
        "sim                              #展示模拟器状态"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "在MindQuantum中，我们可以在`mindquantum.simulator`模块导入模拟器。`Simulator`类可以接受三个参数：\n",
        "\n",
        "- `backend`：所用到的模拟器名称，目前`mindquantum`支持`projectq`作为后端进行模拟。\n",
        "- `n_qbuits`：模拟器所用到的比特数，也就是这里的2。\n",
        "- `seed`：模拟器在运行随机性相关算法时的随机种子，默认为一个随机数，可以不用提供。\n",
        "\n",
        "通过模拟器的输出结果我们可以发现，这是一个`projectq`的2比特模拟器，并且是little endian的。这里little endian的意思是，整个模拟器中，我们都是将比特序号小的比特放在量子态矢量的右边。接下来，输出还说明了模拟器当前所处的量子态是多少，且在模拟器初始化后，当前的量子态默认处于零态。注意，量子模拟器始终会维护一个内部的量子态，当我们作用量子门或者量子线路到模拟器上时，这个量子态会随即发生改变，而当我们只是想获取关于这个量子态的一些信息时，这个量子态则不会改变。这里就涉及到对量子模拟器的两类操作：\n",
        "\n",
        "- 会改变量子态的操作，通常以`apply`开头，主要有如下几个\n",
        "    - `apply_gate`: 作用一个量子门到模拟器上\n",
        "    - `apply_circuit`: 作用一个量子线路到模拟器上\n",
        "    - `apply_hamiltonian`: 将一个哈密顿量作用到模拟器上，注意，此后模拟器的量子态将不再是一个真的量子态\n",
        "    - `set_qs`: 直接设置模拟器的当前量子态\n",
        "    - `reset`: 重置模拟器的状态为|0⟩态\n",
        "- 不会改变量子态的操作，通常以`get`开头，主要有如下几个\n",
        "    - `get_qs`: 获取模拟器的当前量子态\n",
        "    - `get_expectation`: 计算模拟器当前量子态关于某个观察量的期望值\n",
        "    - `get_expectation_with_grad`: 跟上一个接口类似，只不过这个方法还会计算期望值关于参数化量子线路的梯度\n",
        "    - `sampling`: 在当前量子态下，对给定的量子线路进行采样\n",
        "\n",
        "下面我们简单学习模拟器的基本操作。\n",
        "\n",
        "## 作用量子门和量子线路"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "projectq simulator with 2 qubits (little endian).\n",
              "Current quantum state:\n",
              "√2/2¦00⟩\n",
              "√2/2¦01⟩"
            ]
          },
          "execution_count": 3,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "sim = Simulator('projectq', 2)    #声明一个2比特的projectq模拟器\n",
        "sim.apply_gate(H.on(0))           #作用一个Hadamard门到0号比特上\n",
        "sim                               #输出量子模拟器的信息"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "上面我们在量子模拟器的初态上作用了一个Hadamard门，并输出了演化过后的量子态。接下来我们生成一个参数化量子线路，并将其作用到当前的量子态上。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"156.8\" height=\"140\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"156.8\" height=\"140\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq1:\n </text>\n<line x1=\"48.8\" x2=\"136.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"136.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n\n<rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n\n<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nRY\n </text>\n<text x=\"92.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\na\n </text>\n\n</svg></div>",
            "text/plain": [
              "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f913ab81460>"
            ]
          },
          "execution_count": 4,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from mindquantum.core import Circuit   # 导入Circuit模块，用于搭建量子线路\n",
        "\n",
        "circ = Circuit()                       #声明一个空的量子线路\n",
        "circ += H.on(1)                        #向其中添加一个hadamard门，并作用到1号比特上\n",
        "circ += RY('a').on(0)                  #向其中添加一个参数化的RY门，并作用到0号比特上\n",
        "circ.svg()                             #绘制SVG格式的量子线路图片"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "projectq simulator with 2 qubits (little endian).\n",
              "Current quantum state:\n",
              "0.11851349145283657¦00⟩\n",
              "0.6971044056263442¦01⟩\n",
              "0.11851349145283657¦10⟩\n",
              "0.6971044056263442¦11⟩"
            ]
          },
          "execution_count": 5,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "sim.apply_circuit(circ, pr={'a': 1.234})  #作用一个量子线路，当线路是一个参数化量子线路时，我们还需要提供参数值。\n",
        "sim"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "在上面的代码中，我们先生成了一个参数化量子线路`circ`，随后我们将其作用到量子模拟器上，并通过传入字典的方式，将参数`a`设置为`1.234`。最后输出量子模拟器演化出来的量子态。"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 设置并获取模拟器状态\n",
        "\n",
        "我们使用`get_qs(ket=False)`查看当前模拟器的状态，\n",
        "参数ket是一个bool类型的数，它决定了当前模拟器的状态是否以ket的形式返回，ket=False时是以numpy.ndarray形式，ket=True时是以ket形式。默认ket=False。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[0.11851349+0.j 0.69710441+0.j 0.11851349+0.j 0.69710441+0.j]\n"
          ]
        }
      ],
      "source": [
        "print(sim.get_qs())  #查看模拟器状态,以numpy.ndarray形式返回结果"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "0.11851349145283657¦00⟩\n",
            "0.6971044056263442¦01⟩\n",
            "0.11851349145283657¦10⟩\n",
            "0.6971044056263442¦11⟩\n"
          ]
        }
      ],
      "source": [
        "print(sim.get_qs(True))  #查看模拟器状态,以ket形式返回结果"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "在实际写代码过程中，我们常常需要将模拟器指定一个初始态开始演化，这个操作可以使用`set_qs()`实现。\n",
        "\n",
        "例如，我们希望模拟器状态为\n",
        "\n",
        "$$\n",
        "\\frac{\\sqrt{3}}{3}|00⟩+\\frac{\\sqrt{6}}{3}|11⟩\n",
        "$$\n",
        "\n",
        "第一步：我们计算出目标状态的向量形式：\n",
        "\n",
        "$$\n",
        "\\frac{\\sqrt{3}}{3}|00⟩+\\frac{\\sqrt{6}}{3}|11⟩ =\\frac{\\sqrt{3}}{3}\\times\n",
        "\\left(\n",
        "\\begin{array}{l}\n",
        "1\\\\\n",
        "0\n",
        "\\end{array}\n",
        "\\right)\n",
        "\\otimes\n",
        "\\left(\n",
        "\\begin{array}{l}\n",
        "1\\\\\n",
        "0\n",
        "\\end{array}\n",
        "\\right)+\n",
        "\\frac{\\sqrt{6}}{3}\\times\n",
        "\\left(\n",
        "\\begin{array}{l}\n",
        "0\\\\\n",
        "1\n",
        "\\end{array}\n",
        "\\right)\\otimes\n",
        "\\left(\n",
        "\\begin{array}{l}\n",
        "0\\\\\n",
        "1\n",
        "\\end{array}\n",
        "\\right)= \\frac{\\sqrt{3}}{3}\\times\n",
        "\\left(\n",
        "\\begin{array}{l}\n",
        "1\\\\\n",
        "0\\\\\n",
        "0\\\\\n",
        "0\n",
        "\\end{array}\n",
        "\\right)+\n",
        "\\frac{\\sqrt{6}}{3}\\times\n",
        "\\left(\n",
        "\\begin{array}{l}\n",
        "0\\\\\n",
        "0\\\\\n",
        "0\\\\\n",
        "1\n",
        "\\end{array}\n",
        "\\right)=\n",
        "\\left(\n",
        "\\begin{array}{l}\n",
        "\\frac{\\sqrt{3}}{3}\\\\\n",
        "0\\\\\n",
        "0\\\\\n",
        "\\frac{\\sqrt{6}}{3}\n",
        "\\end{array}\n",
        "\\right)\n",
        "$$\n",
        "\n",
        "第二步：我们将这个向量使用`set_qs()`赋值给模拟器，让其作为模拟器的状态："
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "0.5773502691896258¦00⟩\n",
            "0.816496580927726¦11⟩\n"
          ]
        }
      ],
      "source": [
        "sim.set_qs(np.array([3**0.5, 0, 0, 6**0.5]))        #设置模拟器状态，无需归一化\n",
        "print(sim.get_qs(True))                             #查看模拟器状态"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "通过`get_qs()`查看模拟器状态可以发现，当前模拟器状态即为我们希望设置的$\\frac{\\sqrt{3}}{3}|00⟩+\\frac{\\sqrt{6}}{3}|11⟩$。\n",
        "\n",
        "在实际编程过程中，我们常常需要多次模拟电路，通过多开模拟器的方式会导致内存占用非常大，我们可以通过现有模拟器复位的方式来复用模拟器，从而减少内存消耗。\n",
        "\n",
        "我们使用`reset()`来复位模拟器："
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[1.+0.j 0.+0.j 0.+0.j 0.+0.j]\n"
          ]
        }
      ],
      "source": [
        "sim.reset()          #复位模拟器\n",
        "print(sim.get_qs())  #查看模拟器状态"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "可以发现，当前模拟器被复位成了初始的$1|00⟩$态，相当于一个全新的模拟器。\n",
        "\n",
        "因此，我们可以根据自身所需的量子初态，设置对应的量子模拟器，并运行自定义的量子线路。赶紧动手运行你构造出的的第一个量子线路吧！\n",
        "\n",
        "## 量子线路采样\n",
        "\n",
        "线路采样是指对量子线路执行多次模拟测量，统计测量出各种结果出现的频次。**采样不会改变量子线路中的状态**。\n",
        "\n",
        "`sampling(circuit, pr=None, shots=1, seed=None)`是`MindQuantum`中提供的对模拟器进行线路采样方法，它接受四个参数：\n",
        "\n",
        "- `circuit (Circuit)`：希望进行采样的量子线路，注意，该线路中必须包含至少一个测量操作（即采样点）。\n",
        "- `pr (Union[None, dict, ParameterResolver])`：parameter resolver，当 `circuit`是含参线路时，需要给出参数的值。\n",
        "- `shots (int)`：采样的次数，默认为1。\n",
        "- `seed`：采样时的随机种子，默认为一个随机数，可以不用提供。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"276.8\" height=\"140\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"276.8\" height=\"140\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq1:\n </text>\n<line x1=\"48.8\" x2=\"256.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"256.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n\n<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n<rect x=\"132.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n<line x1=\"138.8\" x2=\"166.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<line x1=\"152.8\" x2=\"152.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<rect x=\"192.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" />\n<circle cx=\"212.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" />\n<path d=\"M 200.0 50.4 A 12.8 12.8 0 0 1 225.60000000000002 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" />\n<path d=\"M 216.90184831748593 33.93539030917347 L 225.21569219381655 29.135390309173467 L 225.21569219381655 38.73539030917347 L 221.8901546432843 36.815390309173466 L 214.04707658144957 50.4 L 212.38430780618347 49.44 L 220.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" />\n<rect x=\"192.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" />\n<circle cx=\"212.8\" cy=\"110.4\" r=\"1.6\" fill=\"#ffffff\" />\n<path d=\"M 200.0 110.4 A 12.8 12.8 0 0 1 225.60000000000002 110.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" />\n<path d=\"M 216.90184831748593 93.93539030917347 L 225.21569219381655 89.13539030917347 L 225.21569219381655 98.73539030917347 L 221.8901546432843 96.81539030917347 L 214.04707658144957 110.4 L 212.38430780618347 109.44 L 220.2273858680182 95.85539030917347 Z\" fill=\"#ffffff\" />\n</svg></div>",
            "text/plain": [
              "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f913ab81700>"
            ]
          },
          "execution_count": 10,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# 不含参线路采样：\n",
        "from mindquantum import Measure  # 引入测量门\n",
        "\n",
        "circ = Circuit()                 # 初始化量子线路\n",
        "circ += H.on(0)                  # H门作用在第0位量子比特\n",
        "circ += X.on(1, 0)               # X门作用在第1位量子比特且受第0位量子比特控制\n",
        "circ += Measure('q0').on(0)      # 在0号量子比特作用一个测量，并将该测量命名为'q0'\n",
        "circ += Measure('q1').on(1)      # 在1号量子比特作用一个测量，并将该测量命名为'q1'\n",
        "circ.svg()                       # 绘制SVG格式的量子线路图片"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "<pre style=\"white-space: pre;\"><span style=\"color: #808000; text-decoration-color: #808000\">shots: </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1000</span>\n",
              "<span style=\"color: #808000; text-decoration-color: #808000\">Keys: q1 q0│</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.00</span><span style=\"color: #808000; text-decoration-color: #808000\">   </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.128</span><span style=\"color: #808000; text-decoration-color: #808000\">       </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.256</span><span style=\"color: #808000; text-decoration-color: #808000\">       </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.384</span><span style=\"color: #808000; text-decoration-color: #808000\">       </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.512</span><span style=\"color: #808000; text-decoration-color: #808000\">        </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.64</span>\n",
              "<span style=\"color: #808000; text-decoration-color: #808000\">───────────┼───────────┴───────────┴───────────┴───────────┴───────────┴</span>\n",
              "<span style=\"color: #808000; text-decoration-color: #808000\">         </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">00</span><span style=\"color: #808000; text-decoration-color: #808000\">│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒</span>\n",
              "<span style=\"color: #808000; text-decoration-color: #808000\">           │</span>\n",
              "<span style=\"color: #808000; text-decoration-color: #808000\">         </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">11</span><span style=\"color: #808000; text-decoration-color: #808000\">│▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓</span>\n",
              "<span style=\"color: #808000; text-decoration-color: #808000\">           │</span>\n",
              "<span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\">{</span><span style=\"color: #008000; text-decoration-color: #008000\">'00'</span><span style=\"color: #808000; text-decoration-color: #808000\">: </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">488</span><span style=\"color: #808000; text-decoration-color: #808000\">, </span><span style=\"color: #008000; text-decoration-color: #008000\">'11'</span><span style=\"color: #808000; text-decoration-color: #808000\">: </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">512</span><span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\">}</span>\n",
              "</pre>\n"
            ],
            "text/plain": [
              "shots: 1000\n",
              "Keys: q1 q0│0.00   0.128       0.256       0.384       0.512        0.64\n",
              "───────────┼───────────┴───────────┴───────────┴───────────┴───────────┴\n",
              "         00│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒\n",
              "           │\n",
              "         11│▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓\n",
              "           │\n",
              "{'00': 488, '11': 512}"
            ]
          },
          "execution_count": 11,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "sim.reset()\n",
        "result = sim.sampling(circ, shots=1000)  # 对上面定义的线路采样1000次\n",
        "result"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "MindQuantum还提供了采样结果绘制SVG图的功能："
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"408.4\" height=\"147.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"408.4\" height=\"147.0\" fill=\"#ffffff\" />\n<text x=\"10\" y=\"17.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nShots:\n 1000\n </text>\n<text x=\"10\" y=\"31.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nKeys: q0 q1\n </text>\n<line x1=\"38.4\" x2=\"398.4\" y1=\"62.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"38.4\" x2=\"38.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"40.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.0\n </text>\n<line x1=\"38.4\" x2=\"38.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"98.4\" x2=\"98.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"100.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.102\n </text>\n<line x1=\"98.4\" x2=\"98.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"158.4\" x2=\"158.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"160.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.205\n </text>\n<line x1=\"158.4\" x2=\"158.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"218.4\" x2=\"218.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"220.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.307\n </text>\n<line x1=\"218.4\" x2=\"218.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"278.4\" x2=\"278.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"280.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.41\n </text>\n<line x1=\"278.4\" x2=\"278.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"338.4\" x2=\"338.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"340.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.512\n </text>\n<line x1=\"338.4\" x2=\"338.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<text x=\"29.4\" y=\"85.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n00\n </text>\n<line x1=\"31.4\" x2=\"38.4\" y1=\"85.0\" y2=\"85.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<rect x=\"38.4\" y=\"73.0\" width=\"285.9375\" height=\"24\" id=\"bar_0_1649988007528709375\" fill=\"#5e7ce0\" />\n<text x=\"334.3375\" y=\"85.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_0_1649988007528727097\" fill-opacity=\"0\" >\n488\n </text>\n<text x=\"29.4\" y=\"115.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n11\n </text>\n<line x1=\"31.4\" x2=\"38.4\" y1=\"115.0\" y2=\"115.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<rect x=\"38.4\" y=\"103.0\" width=\"300.0\" height=\"24\" id=\"bar_1_1649988007528766713\" fill=\"#16acff\" />\n<text x=\"348.4\" y=\"115.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_1_1649988007528788991\" fill-opacity=\"0\" >\n512\n </text>\n<animate xlink:href=\"#bar_0_1649988007528709375\" attributeName=\"width\" from=\"0\" to=\"285.9375\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 285.9375\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n<animate xlink:href=\"#bar_1_1649988007528766713\" attributeName=\"width\" from=\"0\" to=\"300.0\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 300.0\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n<animate xlink:href=\"#bar_1_1649988007528766713\" attributeName=\"fill\" from=\"#16acff\" to=\"#fac209\" dur=\"0.15s\" calcMode=\"spline\" values=\"#16acff; #fac209\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<animate xlink:href=\"#bar_text_0_1649988007528727097\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<animate xlink:href=\"#bar_text_1_1649988007528788991\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<text x=\"206.7\" y=\"41.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"middle\" >\nprobability\n </text>\n</svg></div>",
            "text/plain": [
              "<mindquantum.io.display.measure_res_svg_drawer.SVGMeasure at 0x7f9133353e50>"
            ]
          },
          "execution_count": 12,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "result.svg()  # 打印出测量结果的SVG格式"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "我们可以看到，采样1000中，'00'出现了499次，'11'出现了501次。我们搭建的线路实际上制备出了一个贝尔态$\\frac{\\sqrt{2}}{2}|00⟩+\\frac{\\sqrt{2}}{2}|11⟩$。直观上，我们可以看到对该状态进行测量得到'00'的概率为$\\frac{1}{2}$,得到'11'的概率为$\\frac{1}{2}$，采样结果符合概率，细微的误差是由模拟器噪声导致。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"376.8\" height=\"140\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"376.8\" height=\"140\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq1:\n </text>\n<line x1=\"48.8\" x2=\"356.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"356.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n\n<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n<rect x=\"132.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n<line x1=\"138.8\" x2=\"166.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<line x1=\"152.8\" x2=\"152.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n\n<rect x=\"192.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"232.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nRY\n </text>\n<text x=\"232.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\ntheta\n </text>\n\n<rect x=\"192.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" />\n<circle cx=\"212.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" />\n<path d=\"M 200.0 50.4 A 12.8 12.8 0 0 1 225.60000000000002 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" />\n<path d=\"M 216.90184831748593 33.93539030917347 L 225.21569219381655 29.135390309173467 L 225.21569219381655 38.73539030917347 L 221.8901546432843 36.815390309173466 L 214.04707658144957 50.4 L 212.38430780618347 49.44 L 220.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" />\n<rect x=\"292.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" />\n<circle cx=\"312.8\" cy=\"110.4\" r=\"1.6\" fill=\"#ffffff\" />\n<path d=\"M 300.0 110.4 A 12.8 12.8 0 0 1 325.6 110.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" />\n<path d=\"M 316.9018483174859 93.93539030917347 L 325.21569219381655 89.13539030917347 L 325.21569219381655 98.73539030917347 L 321.8901546432843 96.81539030917347 L 314.0470765814496 110.4 L 312.3843078061835 109.44 L 320.2273858680182 95.85539030917347 Z\" fill=\"#ffffff\" />\n</svg></div>",
            "text/plain": [
              "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f9133304520>"
            ]
          },
          "execution_count": 13,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# 含参线路采样：\n",
        "para_circ = Circuit()             # 初始化量子线路\n",
        "para_circ += H.on(0)              # H门作用在第0位量子比特\n",
        "para_circ += X.on(1, 0)           # X门作用在第1位量子比特且受第0位量子比特控制\n",
        "para_circ += RY('theta').on(1)    # RY(theta)门作用在第2位量子比特\n",
        "para_circ += Measure('0').on(0)   # 在0号量子比特作用一个测量，并将该测量命名为'q0'\n",
        "para_circ += Measure('q1').on(1)  # 在1号量子比特作用一个测量，并将该测量命名为'q1'\n",
        "para_circ.svg()                   # 绘制SVG格式的量子线路图片"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"408.4\" height=\"147.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"408.4\" height=\"147.0\" fill=\"#ffffff\" />\n<text x=\"10\" y=\"17.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nShots:\n 1000\n </text>\n<text x=\"10\" y=\"31.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nKeys: 0 q1\n </text>\n<line x1=\"38.4\" x2=\"398.4\" y1=\"62.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"38.4\" x2=\"38.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"40.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.0\n </text>\n<line x1=\"38.4\" x2=\"38.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"98.4\" x2=\"98.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"100.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.103\n </text>\n<line x1=\"98.4\" x2=\"98.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"158.4\" x2=\"158.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"160.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.205\n </text>\n<line x1=\"158.4\" x2=\"158.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"218.4\" x2=\"218.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"220.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.308\n </text>\n<line x1=\"218.4\" x2=\"218.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"278.4\" x2=\"278.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"280.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.41\n </text>\n<line x1=\"278.4\" x2=\"278.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"338.4\" x2=\"338.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"340.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.513\n </text>\n<line x1=\"338.4\" x2=\"338.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<text x=\"29.4\" y=\"85.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n00\n </text>\n<line x1=\"31.4\" x2=\"38.4\" y1=\"85.0\" y2=\"85.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<rect x=\"38.4\" y=\"73.0\" width=\"300.0\" height=\"24\" id=\"bar_0_1649988007818207529\" fill=\"#5e7ce0\" />\n<text x=\"348.4\" y=\"85.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_0_1649988007818237401\" fill-opacity=\"0\" >\n513\n </text>\n<text x=\"29.4\" y=\"115.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n11\n </text>\n<line x1=\"31.4\" x2=\"38.4\" y1=\"115.0\" y2=\"115.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<rect x=\"38.4\" y=\"103.0\" width=\"284.7953216374269\" height=\"24\" id=\"bar_1_1649988007818265369\" fill=\"#16acff\" />\n<text x=\"333.1953216374269\" y=\"115.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_1_1649988007818278671\" fill-opacity=\"0\" >\n487\n </text>\n<animate xlink:href=\"#bar_0_1649988007818207529\" attributeName=\"width\" from=\"0\" to=\"300.0\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 300.0\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n<animate xlink:href=\"#bar_1_1649988007818265369\" attributeName=\"width\" from=\"0\" to=\"284.7953216374269\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 284.7953216374269\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n<animate xlink:href=\"#bar_0_1649988007818207529\" attributeName=\"fill\" from=\"#5e7ce0\" to=\"#fac209\" dur=\"0.15s\" calcMode=\"spline\" values=\"#5e7ce0; #fac209\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<animate xlink:href=\"#bar_text_0_1649988007818237401\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<animate xlink:href=\"#bar_text_1_1649988007818278671\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<text x=\"206.7\" y=\"41.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"middle\" >\nprobability\n </text>\n</svg></div>",
            "text/plain": [
              "<mindquantum.io.display.measure_res_svg_drawer.SVGMeasure at 0x7f91333047f0>"
            ]
          },
          "execution_count": 14,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "sim.reset()\n",
        "result = sim.sampling(para_circ, {'theta': 0},\n",
        "                      shots=1000)  # 将上面定义的线路参数'theta'赋值为0采样1000次\n",
        "result.svg()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "我们可以看到，采样结果中'00'出现了495次，'11'出现了505次。事实上把RY门参数赋值为0，它即为我们熟悉的I门，相当于不对线路做任何操作，因此该采样线路与上面不含参线路本质是同一个，可以观察到二次采样结果几乎相同，符合预期结果。\n",
        "\n",
        "想进一步学习如何对量子线路做测量操作，想了解采样结果分布的理论解释，请点击：[量子测量教程](https://gitee.com/buyulin/mindquantum/blob/master/tutorials/quantum_measurement.ipynb)。\n",
        "\n",
        "若想查询更多关于MindQuantum的API，请点击：[https://mindspore.cn/mindquantum/](https://mindspore.cn/mindquantum/)。"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8.8"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}
